package cn.toleo.activiti.service.impl;


import cn.toleo.activiti.service.ActService;
import cn.toleo.dto.activiti.ActivitiRequestDto;
import cn.toleo.dto.activiti.ActivitiResultDto;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service//toleo;审批Service;2018.1.8
public class ActServiceImpl implements ActService {

    ApplicationContext ctx=new ClassPathXmlApplicationContext("spring/applicationContext-activiti.xml");
    ProcessEngine processEngine =(ProcessEngine)ctx.getBean("pe");

    @Override//2018.2.13;toleo;01部署流程(图画好后部署一次就可以,重复部署以最新版本图为主)
    public  void deploy(String param){
        System.out.println("2352SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS");
            Date date = new Date();
            String s = date.toString();
            processEngine.getRepositoryService().createDeployment().name("狮子技术" + s).addClasspathResource("properties/1452bbb.bpmn").deploy();
    }

    @Override//2018.2.13;toleo;02启动流程,设定初始代办人,设定审批变量
    public ActivitiResultDto start(ActivitiRequestDto activitiRequestDto){
        HashMap<String, Object> variables = new HashMap<>();
        //申请代办人设定
        variables.put("Approve_userID",activitiRequestDto.getPerson());
        variables.put("money",activitiRequestDto.getMoney());
        variables.put("approveComment",activitiRequestDto.getApproveComment());

        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(activitiRequestDto.getDeployKey(), variables);
        ActivitiResultDto resultDto= new ActivitiResultDto();
        resultDto.setMsg("申请成功,申请人是"+"'"+activitiRequestDto.getPerson()+"'");
        return  resultDto;
    }

    @Override//2018.2.13;toleo;03根据人,候选人查看任务
    public  List<ActivitiResultDto> queryByPerson(String person){
        List<ActivitiResultDto> activitiResultDtos = new ArrayList<>();
        String assignee=person;
        List<Task> Tasks = processEngine.getTaskService().createTaskQuery().taskCandidateOrAssigned(assignee).list();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        for (Task t:Tasks){
            ActivitiResultDto activitiResultDto = new ActivitiResultDto();
            activitiResultDto.setTaskId(t.getId());
            activitiResultDto.setTaskName(t.getName());
            activitiResultDto.setExecution_Listener_Msg((String) runtimeService.getVariable(t.getProcessInstanceId(), "execution_Listener_Msg"));
            activitiResultDto.setTask_Listener_Msg((String) runtimeService.getVariable(t.getProcessInstanceId(), "task_Listener_Msg"));
            activitiResultDtos.add(activitiResultDto);

        }
        //结果
        return  activitiResultDtos;
    }

    @Override//2018.2.13;toleo;04办理任务;设置流程变量;得到processInstanceId,executionId
    public ActivitiResultDto action(ActivitiRequestDto activitiRequestDto){
        String taskId=activitiRequestDto.getTaskId();
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("handleComment",activitiRequestDto.getHandleComment());
        Task task = processEngine.getTaskService().createTaskQuery().taskId(activitiRequestDto.getTaskId()).singleResult();
        String processInstanceId = task.getProcessInstanceId();
        //核心
        processEngine.getTaskService().complete(taskId,variables);

        ActivitiResultDto activitiResultDto = new ActivitiResultDto();
        activitiResultDto.setProcessInstanceId(processInstanceId);
        return  activitiResultDto;
    }


    @Override//2018.3.1;toleo;05办理recTask任务;
    public void recTask(ActivitiRequestDto activitiRequestDto) {

        String executionId = activitiRequestDto.getExecutionId();
        processEngine.getRuntimeService().signal(executionId);
    }

    @Override//2018.3.1;toleo;06根据activitiId查询 executionId
    public  List<ActivitiResultDto> queryByaActivitiId(String param){
        List<ActivitiResultDto> activitiResultDtos = new ArrayList<>();

        String activitiId=param;
        List<Execution> executions = processEngine.getRuntimeService().createExecutionQuery().activityId(activitiId).list();
        for (Execution e:executions){
            ActivitiResultDto activitiResultDto = new ActivitiResultDto();
            activitiResultDto.setExecutionId(e.getId());
            activitiResultDto.setProcessInstanceId(e.getProcessInstanceId());
            activitiResultDtos.add(activitiResultDto);
        }
        //结果
        return  activitiResultDtos;
    }



}
